<?php

namespace app\util;

/*
 * 印刷文字识别-身份证识别/OCR文字识别
 */
class Ocr
{
    //请求路径
    private $api_send_url;
    private $appcode;
    /*
     * 构造方法
     */
    public function __construct()
    {
        $this->api_send_url = 'https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json';
        $this->appcode = 'c220988574424ef281cd85e5a5d6b5a0';
    }
    
    /*
     * 根据身份证图片获取身份证号和姓名
     * 
     */
    public function ocrIdcard($base64) {
        $url = $this->api_send_url;
        $appcode = $this->appcode;
        //$file = "你的文件路径";
        //如果输入带有inputs, 设置为True，否则设为False
        $is_old_format = false;
        //如果没有configure字段，config设为空
        $config = array(
            "side" => "face"
        );
        //$config = array()


//        if ($fp = fopen($file, "rb", 0)) {
//            $binary = fread($fp, filesize($file)); // 文件读取
//            fclose($fp);
//            $base64 = base64_encode($binary); // 转码
//        }
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8");
        $querys = "";
        if ($is_old_format == TRUE) {
            $request = array();
            $request["image"] = array(
                "dataType" => 50,
                "dataValue" => "$base64"
            );

            if (count($config) > 0) {
                $request["configure"] = array(
                    "dataType" => 50,
                    "dataValue" => json_encode($config)
                );
            }
            $body = json_encode(array("inputs" => array($request)));
        } else {
            $request = array(
                "image" => "$base64"
            );
            if (count($config) > 0) {
                $request["configure"] = json_encode($config);
            }
            $body = json_encode($request);
        }
        $method = "POST";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        $result = curl_exec($curl);
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $rheader = substr($result, 0, $header_size);
        $rbody = substr($result, $header_size);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($httpCode == 200) {
            if ($is_old_format) {
                $output = json_decode($rbody, true);
                $result_str = $output["outputs"][0]["outputValue"]["dataValue"];
            } else {
                $result_str = $rbody;
            }
            //printf("result is :\n %s\n", $result_str);
            $sendResult = [
                'code' => '0',
                'msg' => '验证成功',
                'data' => $result_str,
            ];
            return $sendResult;
        } else {
//            printf("Http error code: %d\n", $httpCode);
//            printf("Error msg in body: %s\n", $rbody);
//            printf("header: %s\n", $rheader);
            $sendResult = [
                'code' => '1',
                'msg' => '验证失败',
                'data' => null,
            ];
            return $sendResult;
        }
    }

}